package com.mimc.framework.jwt;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.jwt.JWT;

import com.mimc.core.model.ImUser;
import com.mimc.core.service.ImUserService;
import com.mimc.framework.core.RequestContextHolder;
import com.jfinal.aop.Aop;


import javax.servlet.http.HttpServletRequest;
import java.util.Date;

public class ApiTokenUtils {

    /**
     *
     * @param userId 用户Id
     * @param sign 密钥
     * @param expireTime 过期时间
     * @return
     */
    public static String genToken(Long userId,String sign,Integer expireTime){
        Date expiresAt= DateUtil.offsetSecond(new Date(),expireTime);
        String token = JWT.create()
                .setPayload("userId", userId)
                .setKey(sign.trim().getBytes())
                .setExpiresAt(expiresAt)
                .sign();
        return token;
    }

    //获取当前登录的用户信息
    public static ImUser getCurrentUser(){
        Long userId=getCurrentUserId();
        if(userId==null){
            return null;
        }
        ImUserService service = Aop.get(ImUserService.class);
        return service.findById(userId);
    }
    public static Long getCurrentUserId(){
        try{
            HttpServletRequest request = RequestContextHolder.get();
            String token = request.getHeader("token");
            if (StrUtil.isNotBlank(token)){
                JWT jwt = JWT.of(token);
                Long userId= Long.valueOf(jwt.getPayload("userId").toString()) ;
                return userId;
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;

        }
        return null;
    }
    public static Boolean verifyToken(String token,String key){
        return JWT.of(token).setKey(key.trim().getBytes()).verify();
    }
    //获取客户端操作系统 ios android
    public static String getDeviceOs(){
        HttpServletRequest request = RequestContextHolder.get();
        String os = request.getHeader("os");
        return os;
    }
    //获取手机品牌
    public static String getDeviceBrand(){
        HttpServletRequest request = RequestContextHolder.get();
        String brand = request.getHeader("brand");
        return brand;
    }
    //获取应用版本
    public static String getAppVersion(){
        HttpServletRequest request = RequestContextHolder.get();
        String version = request.getHeader("version");
        return version;
    }
    //获取应用版本号
    public static String getAppBuildNumber(){
        HttpServletRequest request = RequestContextHolder.get();
        String buildNumber = request.getHeader("buildNumber");
        return buildNumber;
    }
    public static String getLang(){
        HttpServletRequest request = RequestContextHolder.get();
        String lang = request.getHeader("lang");
        String queryLang=request.getParameter("lang");
        if(io.jboot.utils.StrUtil.isNotEmpty(queryLang)){
            return queryLang;
        }
        if(io.jboot.utils.StrUtil.isNotEmpty(lang)){
            return lang;
        }

        return null;
    }
    //访问来源
    public static String getSource(){
        HttpServletRequest request = RequestContextHolder.get();
        String source = request.getHeader("source");
        String querySource=request.getParameter("source");
        if(io.jboot.utils.StrUtil.isNotEmpty(source)){
            return source;
        }
        if(io.jboot.utils.StrUtil.isNotEmpty(querySource)){
            return querySource;
        }
        return "uniapp";
    }
}
